import 'dart:async';

import 'package:dio/dio.dart';
import 'package:flutter/material.dart';
import 'package:fluttertz/jsonserialize/cjwt_list/data.dart';
import 'package:fluttertz/pages/cjwt/widgets/cjwt_row.dart';
import 'package:fluttertz/utils/global.dart';

class Cjwt extends StatefulWidget {
  Cjwt({Key key}) : super(key: key);

  _CjwtState createState() => _CjwtState();
}

class _CjwtState extends State<Cjwt> {
  double bottom = G.screenPadding().bottom;

  AppBar createAppBar() {
    return null;
  }

  static double _nestedScrollOffet = 0;
  final ScrollController _nestedScrollController = new ScrollController();

  List<Widget> cjwtListWidgets = [];

  @override
  void initState() {
    _nestedScrollController.addListener(() {
      setState(() {
        _nestedScrollOffet = _nestedScrollController.offset;
      });
    });

    Future.delayed(Duration.zero, () {
      _init(context);
    });

    super.initState();
  }

  _init(BuildContext context) async {
    G.loading.show(context);
    try {
      Response response;
      Dio dio = Dio();
      String url = "https://aps.acciot.com.tw/api/taizhu/cjwt";
      response = await dio.get(url);
      if (response == null) {
        return G.toast('獲取常見問題失敗！');
      }

      CjwtList cjwtList = CjwtList.fromJson(response.data);
      int cjwtListLen = cjwtList.data.length;

      List<Widget> cjwtListWidgetsTemp = [];

      cjwtList.data.asMap().forEach((int index, CjwtListDatum cjwtone) {
        cjwtListWidgetsTemp.add(CjwtRow(
          onPress: (BuildContext context, int id) {},
          data: cjwtone,
        ));
      });

      setState(() {
        cjwtListWidgets = cjwtListWidgetsTemp;
      });

      G.loading.hide(context);
    } catch (e) {
      print(e);
      G.loading.hide(context);
    }
  }

  @override
  Widget build(BuildContext context) {
    return Container(
        decoration: BoxDecoration(
            image: DecorationImage(
          image: AssetImage("lib/assets/images/tz/bg.png"),
          fit: BoxFit.cover,
        )),
        child: Scaffold(
          backgroundColor: Colors.transparent,
          appBar: customAppbar(
            title: '常見問題',
            context: context, //null不显示返回按钮
          ),
          body: Stack(
            children: <Widget>[
              Container(
                color: Colors.transparent,
                padding: EdgeInsets.only(left: 25, right: 25),
                child: ListView(
                  padding: EdgeInsets.only(bottom: 80 + bottom),
                  children: cjwtListWidgets,
                ),
              ),
            ],
          ),
        ));
  }
}
